window.onload = function () {
    //获取搜索结果
//	$.ajax({
//		type: 'get',
//		url: 'http://api.staging.shidaiwl.cn/api/v1/game/member/game',
//		/*
//		 * 参数
//		?page=1&per_page=5
//		page: [可选]默认值 1 是首页；传 0 表示最后一页
//		per_page: [可选]默认值 5 每页有五条记录
//		category: [可选] 分类
//		name:[可选] 游戏名称
//		*/
//		data: JSON.stringify({
//			"name": window.name
//		}),
//
//		json: true,
//		contentType: 'application/json',
//		success: function(data) {
//			if(isNull(data)==''){
//				alert('获取获取搜索结果数据出错，请稍候再试~~');
//				return;
//			}
//			console.log('获取搜索结果  success,data:' + data);
//			dealSearchResultList(data);
//		},
//		error: function(data) {
//			console.log('获取搜索结果  error,data:' + data);
//		}
//	});

//			获取首页热门排行数据
//			?page=1&per_page=5
//			page: [可选]默认值 1 是首页；传 0 表示最后一页
//			per_page: [可选]默认值 5 每页有五条记录
//			category: [可选] 分类
//			name:[可选] 游戏名称

    request(base_url + '/game/member/game', {"type": "map", "content": {"name": localStorage.getItem('search')}})
        .done(function (data) {
            dealSearchResultList(data);
        })
        .fail(function () {
            console.log('dealSearchResultList error');
        });

    function dealSearchResultList(json) {
        if (json.total == '0') {
            var no_result = document.getElementById('no_result');
            no_result.style.visibility = 'visible';
            return;
        }

        var oUl = document.getElementById('hot_ul');

        for (var hotgame_list_num = 0; hotgame_list_num < json.items.length; hotgame_list_num++) {
            var newli = document.createElement('li');
            newli.className = 'mui-table-view-cell mui-media';
            /*获取星星显示的span*/
            var starSpan = '';
            var score_avg = json.items[hotgame_list_num].score_avg; //分数
            var score_avg_x10 = score_avg * 10;
            var score_avg_x10_ = score_avg_x10 % 10;
            if (score_avg_x10_ > 0 && score_avg_x10_ <= 5) {
                needHalfStar = true;
            } else {
                needHalfStar = false;
            }
            //							alert(needHalfStar);
            for (var home_hotgame_starnum = 0; home_hotgame_starnum < 5; home_hotgame_starnum++) {
                if (home_hotgame_starnum < parseInt(score_avg)) {
                    starSpan += '<span class="iconfont icon-xing1"></span>';
                } else {
                    if (home_hotgame_starnum == parseInt(score_avg) && needHalfStar) {
                        starSpan += '<span class="iconfont icon-ban_xing2"></span>';
                    } else {
                        if (home_hotgame_starnum == parseInt(score_avg) && score_avg_x10_ > 5) {
                            starSpan += '<span class="iconfont icon-xing1"></span>';
                        } else {
                            starSpan += '<span class="iconfont icon-xingxing"></span>';
                        }
                    }
                }
            }

            var gametypeSpan = '';
            var ttj = '{"gametypes":["竞技","休闲"]}';
            var gametypejson = eval('(' + ttj + ')');
            for (var gametypenum = 0; gametypenum < gametypejson.gametypes.length; gametypenum++) {
                var span = document.createElement('span');
                span.className = 'game_type';
                span.innerText = gametypejson.gametypes[gametypenum];
                gametypeSpan += span.outerHTML;
            }

            var tmpHtml = '<a href="javascript:;"><img class="mui-media-object mui-pull-left" src="' +
                json.items[hotgame_list_num].icon + //图片地址
                '"><div class="mui-media-body main_title">' +
                json.items[hotgame_list_num].game_name + //主标题
                '-<span class="allgame_column_title_tail">' +
                json.items[hotgame_list_num].game_title + //副标题
                '</span></div><div class="mui-media-body"><div style="display: inline;">' +
                starSpan + //星星
                '<span class="final_score">&nbsp;&nbsp;' +
                '' + //分数
                '</span></div><span class="mui-pull-right iconfont icon-xiazai hot_download button_download" data_android_link="' +
                json.items[hotgame_list_num].android_link +//安卓链接
                '" data_ios_link ="' +
                json.items[hotgame_list_num].ios_link +//IOS链接
                '">下载</span></div><div class="mui-media-body mui-ellipsis">' +
                gametypeSpan + //类型
                '</div></a>';

            newli.innerHTML = tmpHtml;
            newli.id = json.items[hotgame_list_num].id;
            oUl.appendChild(newli);
        }
    }

    function initClick() {

        mui('#hot_ul').on('tap', 'li', function () {
            window.name = this.id;
            window.location.href = 'detail.html';
            return false;
        });
        mui('#hot_ul').on('tap', '.button_download', function () {
            download(isIOSClient(), this.getAttribute('data_ios_link'), this.getAttribute('data_android_link'));
            return false;
        });
    }

    initClick();
};